$(document).ready(function() {
   $('#more_folder').on('click', '#selectmore_folder', function(){
      if(!$('#more_folder').hasClass('open')) {
         $('#more_folder').addClass('open');
         if(!$('#folder_dropdown').html()) {
            $.ajax({
               type: 'GET',
               url: OC.filePath('resumes', 'ajax', 'getfolderdropdown.php'),
               cache: false, 
               beforeSend: function() {
                  $('#folder_dropdown').html('').css('minHeight', 50).show();
                  if(!showLoading($("#folder_dropdown"), null, null, {opacity : "1"}))
                     return false;
               },
               success: function(result) {
                  $('#folder_dropdown').css('minHeight', 0).html(result);
               }
            });
         } else {
            $('#folder_dropdown').show();
         }
      } else {
         $('#more_folder').removeClass('open');
         $('#folder_dropdown').hide();
      }
   }).on('mousedown', '#selectmore_folder', function(event){
      event.stopPropagation();
   }).on('click', '.editi', function(event){
      event.stopPropagation();
      
      var fDiv = $(this).closest(".canedit");
      $("a", fDiv).hide();
      $("div.btns", fDiv).hide();
      $("input.fname", fDiv).show();
      $("div.editbtns", fDiv).show();
      $('#folder_dropdown').addClass('keepopen');
   }).on('click', '.closei', function(event){
      event.stopPropagation();
      
      var target = $(this);
      var fDiv = $(this).closest(".canedit");
      var fid = fDiv.data("fid");
      
      $('#folder_dropdown').addClass('keepopen');
      
      showConfirmDialog('确认删除该文件夹么？',function() {
         $.ajax({
            type: "POST",
            url: OC.filePath('resumes', 'ajax', 'removefolder.php'),
            data: {fid: fid},
            beforeSend: function(xhr, settings) {
               if(!showLoading(target, settings, null, {button:true}))
                  return false;
            },
            success: function(result){
               if (!isLoggedIn(result, target)) {
                  window.location = redirect_url;
                  return;
               }
               
               if(!$.isPlainObject(result)) {
                  result = $.parseJSON(result);
               }
               
               if(result.status === 'success') {
                  if(window.location.href.indexOf("dir="+fid) == -1){
                     $.ajax({
                        type: 'GET',
                        url: OC.filePath('resumes', 'ajax', 'getfolderdropdown.php'),
                        cache: false, 
                        beforeSend: function() {
                           if(!showLoading($("#folder_dropdown")))
                              return false;
                        },
                        success: function(result) {
                           $('#folder_dropdown').html(result).removeClass('keepopen');
                        }
                    });
                  }else{
                     window.history.back();
                  } 
               } else {
                  showMessagePanel(result.data.message, target, true, {placement:'right'});
               }
            }
         });
      }, function(){
         $('#folder_dropdown').removeClass('keepopen');
      });
   }).on('click', '.ok', function(event){
      event.stopPropagation();
      
      var fDiv = $(this).closest(".canedit");
      var fid = fDiv.data("fid");
      var fname = $('input.fname', fDiv);
      
      if ($.trim(fname.val()).length <=0) {
         showMessagePanel("请输入文件夹名称", fname, true, {placement:'left'});
         return false;
      }
      
      if (compact_strlen(fname.val()) > 10) {
         showMessagePanel("文件夹名称不能超过10个汉字", fname, true, {placement:'left'});
         return false;
      }
      
      $.ajax({
         type: "POST",
         url: OC.filePath('resumes', 'ajax', 'savefolder.php'),
         data: {fid: fid, fname: fname.val()},
         beforeSend: function(xhr, settings) {
            if(!showLoading(fDiv, settings, null, {button:true}))
               return false;
         },
         success: function(result){
            if (!isLoggedIn(result, fDiv)) {
               window.location = redirect_url;
               return;
            }
            
            if(!$.isPlainObject(result)) {
               result = $.parseJSON(result);
            }
            
            if(result.status === 'success') {
               $.ajax({
                  type: 'GET',
                  url: OC.filePath('resumes', 'ajax', 'getfolderdropdown.php'),
                  cache: false, 
                  success: function(result) {
                     $('#folder_dropdown').html(result).removeClass('keepopen');
                  }
               });
            } else {
               showMessagePanel(result.data.message, fDiv, true, {placement:'right'});
            }
         }
      });
   }).on('click', '.cancel', function(event){
      event.stopPropagation();
      
      var fDiv = $(this).closest(".canedit");
      $("a", fDiv).show();
      $("div.btns", fDiv).show();
      $("input.fname", fDiv).hide();
      $("div.editbtns", fDiv).hide();
      $('#folder_dropdown').removeClass('keepopen');
   }).on('mouseenter', '.canedit', function(){
      if($('a', $(this)).is(':visible')) {
         $('.btns', $(this)).show();
      }
   }).on('mouseleave', '.canedit', function(){
      if($('a', $(this)).is(':visible')) {
         $('.btns', $(this)).hide();
      }
   });
   
   $('#infolder_modal').on('click', '.createfolder', function(){
      var cDiv = $(this).closest('.createfolder_div');
      $(this).hide();
      $("input.fname", cDiv).show();
      $(".editbtns", cDiv).show();
   }).on('click', '.createfolder_div .ok', function(event){
      event.stopPropagation();
      
      var cDiv = $(this).closest('.createfolder_div');
      var fname = $('input.fname', cDiv);
      var ids = $('#infolder_ids').val();
      
      if ($.trim(fname.val()).length <=0) {
         showMessagePanel("请输入文件夹名称", fname, true, {placement:'left'});
         return false;
      }
      
      if (compact_strlen(fname.val()) > 10) {
         showMessagePanel("文件夹名称不能超过10个汉字", fname, true, {placement:'left'});
         return false;
      }
      
      $.ajax({
         type: "POST",
         url: OC.filePath('resumes', 'ajax', 'savefolder.php'),
         data: {fid: 0, fname: fname.val()},
         beforeSend: function(xhr, settings) {
            if(!showLoading(cDiv, settings, null, {button:true}))
               return false;
         },
         success: function(result){
            if (!isLoggedIn(result, cDiv)) {
               window.location = redirect_url;
               return;
            }
            
            if(!$.isPlainObject(result)) {
               result = $.parseJSON(result);
            }
            
            if(result.status === 'success') {
               $.ajax({
                  type: 'GET',
                  url: OC.filePath('resumes', 'ajax', 'getinfolderlist.php'),
                  data: {ids: ids},
                  cache: false, 
                  success: function(result) {
                     $('#infolder_body').html(result);
                  }
               });
            } else {
               showMessagePanel(result.data.message, cDiv, true, {placement:'right'});
            }
         }
      });
   }).on('click', '.createfolder_div .cancel', function(){
      var cDiv = $(this).closest('.createfolder_div');
      $("input.fname", cDiv).val('').hide();
      $(".editbtns", cDiv).hide();
      $('a.createfolder', cDiv).show();
   }).on('click', 'a.selectitem', function(){
      if(!$(this).hasClass('cur_a')) {
         $('#infoldertable a.selectitem').removeClass('cur_a');
         $(this).addClass('cur_a');
      } else {
         $(this).removeClass('cur_a');
      }
   }).on('click', '.saveinfolder', function(){
      var fDiv = $('#infolder_modal');
      
      $('.server', fDiv).html('');
      if ($('a.selectitem.cur_a').length <=0) {
         $('.server', fDiv).html('请选择文件夹');
         return false;
      }
      
      var target = $(this);
      var ids = $('#infolder_ids').val();
      var fid = $('a.selectitem.cur_a').eq(0).data('fid');
      
      $.ajax({
         type: "POST",
         url: OC.filePath('resumes', 'ajax', 'saveinfolder.php'),
         data: {ids: ids, fid: fid},
         beforeSend: function(xhr, settings) {
            if(!showLoading(fDiv))
               return false;
         },
         success: function(result){
            if (!isLoggedIn(result, target)) {
               window.location = redirect_url;
               return;
            }
            
            if(!$.isPlainObject(result)) {
               result = $.parseJSON(result);
            }
            
            if(result.status === 'success') {
               $('#infolder_modal').modal('hide');
               OC.Notification.showHtml('移动简历成功。', 3000);
            } else {
               $('.server', fDiv).html(result.data.message);
            }
         }
      });
   });
   
   $('#foldermanage_modal').on('mouseenter', 'a.link', function(){
      var td = $(this).closest('td');
      if($(this).is(':visible')) {
         $('.btns', td).show();
      }
   }).on('mouseleave', 'td', function(){
      if($('a.link', $(this)).is(':visible')) {
         $('.btns', $(this)).hide();
      }
   }).on('click', '.editi', function(){
      var td = $(this).closest("td");
      $("a.link", td).hide();
      $(".btns", td).hide();
      $("input.fname", td).show();
      $(".editbtns", td).show();
   }).on('click', '.closei', function(event){
      event.stopPropagation();
      
      var target = $(this);
      var td = $(this).closest("td");
      var fid = td.data("fid");
      
      $('#foldermanage_modal').modal('hide');
      showConfirmDialog('确认删除该文件夹么？',function() {
         $('#foldermanage_modal').modal('show');
         $.ajax({
            type: "POST",
            url: OC.filePath('resumes', 'ajax', 'removefolder.php'),
            data: {fid: fid},
            beforeSend: function(xhr, settings) {
               if(!showLoading(target, settings, null, {button:true}))
                  return false;
            },
            success: function(result){
               if (!isLoggedIn(result, target)) {
                  window.location = redirect_url;
                  return;
               }
               
               if(!$.isPlainObject(result)) {
                  result = $.parseJSON(result);
               }
               
               if(result.status === 'success') {
                  $.ajax({
                     type: 'GET',
                     url: OC.filePath('resumes', 'ajax', 'getcustomfolderlist.php'),
                     cache: false, 
                     beforeSend: function() {
                        if(!showLoading($("#foldermanage_body")))
                           return false;
                     },
                     success: function(result) {
                        $('#foldermanage_body').html(result);
                     }
                  });
               } else {
                  showMessagePanel(result.data.message, target, true, {placement:'right'});
               }
            }
         });
      }, function(){
         $('#foldermanage_modal').modal('show');
      });
   }).on('click', '.ok', function(event){
      event.stopPropagation();
      
      var td = $(this).closest("td");
      var fid = td.data("fid");
      var fname = $('input.fname', td);
      
      if ($.trim(fname.val()).length <=0) {
         showMessagePanel("请输入文件夹名称", fname, true, {placement:'left'});
         return false;
      }
      
      if (compact_strlen(fname.val()) > 10) {
         showMessagePanel("文件夹名称不能超过10个汉字", fname, true, {placement:'left'});
         return false;
      }
      
      $.ajax({
         type: "POST",
         url: OC.filePath('resumes', 'ajax', 'savefolder.php'),
         data: {fid: fid, fname: fname.val()},
         beforeSend: function(xhr, settings) {
            if(!showLoading(td, settings, null, {button:true}))
               return false;
         },
         success: function(result){
            if (!isLoggedIn(result, td)) {
               window.location = redirect_url;
               return;
            }
            
            if(!$.isPlainObject(result)) {
               result = $.parseJSON(result);
            }
            
            if(result.status === 'success') {
               $.ajax({
                  type: 'GET',
                  url: OC.filePath('resumes', 'ajax', 'getcustomfolderlist.php'),
                  cache: false, 
                  success: function(result) {
                     $('#foldermanage_body').html(result);
                  }
               });
            } else {
               showMessagePanel(result.data.message, td, true, {placement:'right'});
            }
         }
      });
   }).on('click', '.cancel', function(event){
      event.stopPropagation();
      
      var td = $(this).closest("td");
      $("a.link", td).show();
      $(".btns", td).show();
      $("input.fname", td).hide();
      $(".editbtns", td).hide();
   });
   
   $(document).on('click', '#createfolder_btn', function(event){
      event.stopPropagation();
      $("#creater_newfolder").modal();
      $('#folder_dropdown').addClass('keepopen');
   });
   
   $('#creater_newfolder').on('hidden', function () {
      $('#folder_dropdown').removeClass('keepopen');
   });
   
   $('#newfolderform').ajaxForm({
      beforeSubmit: function(data, form, options) {
         $(".server", form).html('');
         
         var fname = $("input.fname", form);
         
         if ($.trim(fname.val()).length <=0) {
            $(".server", form).html("请输入文件夹名称");
            return false;
         }
         
         if (compact_strlen(fname.val()) > 10) {
            $(".server", form).html("文件夹名称不能超过10个汉字");
            return false;
         }
         
         if (!showLoading(form, options, null, {button:true}))
            return false;
      },
      success: function(result, status, xhr, form) {
         if (!isLoggedIn(result, $('input.submit', form))) {
            window.location = redirect_url;
            return;
         }
         
         if(!$.isPlainObject(result)) {
            result = $.parseJSON(result);
         }
         
         if(result.status === 'success') {
            $('#creater_newfolder').modal('hide');
            form.get(0).reset();
            
            $.ajax({
               type: 'GET',
               url: OC.filePath('resumes', 'ajax', 'getfolderdropdown.php'),
               cache: false, 
               beforeSend: function() {
                  if(!showLoading($("#folder_dropdown")))
                     return false;
               },
               success: function(result) {
                  $('#folder_dropdown').html(result);
               }
            });
         } else {
            $(".server", form).html(result.data.message);
         }
      }
   });
});
